Some examples of plotly plots
library(tidyverse)
## -- Attaching packages ----------------------------------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.0.0 √ purrr 0.2.5
## √ tibble 1.4.2 √ dplyr 0.7.6
## √ tidyr 0.8.1 √ stringr 1.3.1
## √ readr 1.1.1 √ forcats 0.3.0
## -- Conflicts -------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(viridis)
## Loading required package: viridisLite
library(p8105.datasets)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
set.seed(1)
data(nyc_airbnb)
nyc_airbnb =
nyc_airbnb %>%
mutate(rating = review_scores_location / 2) %>%
select(boro = neighbourhood_group, neighbourhood, rating, price, room_type,
latitude, longitude) %>%
filter(!is.na(rating),
boro == "Manhattan",
room_type == "Entire home/apt",
price %in% 100:500) %>%
sample_n(5000)
Create plotly scatterplot. Scatter plots have different modes. Used mutate to create a new variable called text_label. indicates new line for rating. Mapping text_lable to text.
nyc_airbnb %>%
mutate(text_label = str_c("Price: $", price, '\nRating: ', rating)) %>%
plot_ly(x = ~longitude, y = ~latitude, type = "scatter", mode = "markers",
alpha = 0.5,
color = ~price,
text = ~text_label)
common_neighborhoods =
nyc_airbnb %>%
count(neighbourhood, sort = TRUE) %>%
top_n(8) %>%
select(neighbourhood)
## Selecting by n
inner_join(nyc_airbnb, common_neighborhoods,
by = "neighbourhood") %>%
mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>%
plot_ly(y = ~price, color = ~neighbourhood, type = "box",
colors = "Set2")
nyc_airbnb %>%
count(neighbourhood) %>%
mutate(neighbourhood = fct_reorder(neighbourhood, n)) %>%
plot_ly(x = ~neighbourhood, y = ~n, color = ~neighbourhood, type = "bar")
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors
The code below recreates our scatterplot using ggplot. We then used ggplotly, converting a ggplot object straight to an interactive graphic.
scatter_ggplot =
nyc_airbnb %>%
ggplot(aes(x = longitude, y = latitude, color = price)) +
geom_point(alpha = 0.25) +
scale_color_viridis() +
coord_cartesian() +
theme_classic()
ggplotly(scatter_ggplot)
boxplot can be created in a similiar way
box_ggplot =
inner_join(nyc_airbnb, common_neighborhoods,
by = "neighbourhood") %>%
mutate(neighbourhood = fct_reorder(neighbourhood, price)) %>%
ggplot(aes(x = neighbourhood, y = price, fill = neighbourhood)) +
geom_boxplot() +
theme_classic() +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
ggplotly(box_ggplot)